<template>
	<view class="page">
		<view class="list">
			<view class="listX" v-for="(v, k) in listData" :key="k">
				<view class="listX_le">
					<image :src="v.head_img" mode="aspectFill" :lazy-load="true"></image>
					<text>{{v.user_nickname}}</text>
				</view>
				<view class="listX_ri" @tap="remove(v.black_id)">
					<view>移除</view>
				</view>
			</view>
		</view>
		<view v-if="listData.length == 0" style="padding: 20rpx 0;">
			<u-empty text="没有更多了" mode="list"></u-empty>
		</view>
		<view style="padding: 20rpx 0;">
			<u-loadmore v-if="last_page > 1" :status="status" :icon-type="iconType" :load-text="loadText" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				listData: [],
				last_page: 1,
				page: 1,
				refresh: false,
				status: 'loadmore',
				iconType: 'flower',
				loadText: {
					loadmore: '轻轻上拉',
					loading: '努力加载中',
					nomore: '没有更多了'
				}
			}
		},
		onLoad() {

		},
		onShow() {
			uni.showLoading({
				title: '加载中'
			})
			this.page = 1
			this.refresh = true
			this.blackInfo()
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.page = 1
			this.refresh = true
			this.blackInfo()
		},
		// 上拉加载
		onReachBottom() {
			if (this.page >= this.last_page) return;
			this.status = 'loading';
			this.page++;
			this.refresh = false
			setTimeout(() => {

				if (this.page >= this.last_page) this.status = 'nomore';
				else this.status = 'loading';
				this.blackInfo()
			}, 500)
		},
		methods: {
			// 获取消费记录
			blackInfo() {
				this.$api.post(global.apiUrls.myBlack, {
					type: 1,
					page: this.page
				}).then(res => {
					// console.log(res)
					uni.hideLoading()
					uni.stopPullDownRefresh()
					if (res.data.code == 1) {
						this.last_page = res.data.data.last_page
						if (this.refresh) {
							this.listData = res.data.data.data
							this.refresh = false
						} else {
							this.listData = this.listData.concat(res.data.data.data)

						}

					} else {
						// this.$message.info(res.data.msg)
					}
				})
			},
			// 移除黑名单
			remove(black_id) {
				this.$api.post(global.apiUrls.removeBlack, {
					id: black_id
				}).then(res => {
					if(res.data.code == 1) {
						this.$message.info(res.data.msg)
						for (let i =0 ;i < this.listData.length; i++) {
							if (this.listData[i].black_id == black_id) {
								this.listData.splice(i, 1)
							}
						}
					} else {
						this.$message.info(res.data.msg)
					}
				})
			}
		},

	}
</script>

<style lang="scss" scoped>
	.page {
		min-height: 100vh;
		background-color: #FFFFFF;
		color: #333333;

		.list {
			padding: 0 32rpx;

			.listX {
				padding: 32rpx 0;
				border-bottom: 1rpx solid #f2f2f1;
				display: flex;

				.listX_le {
					flex: 5;

					image {
						width: 88rpx;
						height: 88rpx;
						border-radius: 50%;
						vertical-align: middle;
					}

					text {
						margin-left: 24rpx;
						font-weight: bold;
					}
				}

				.listX_ri {
					flex: 5;
					padding-top: 15rpx;

					view {
						text-align: center;
						width: 156rpx;
						height: 56rpx;
						line-height: 56rpx;
						background: #FFFFFF;
						border: 1rpx solid #BFBFBF;
						opacity: 1;
						border-radius: 8rpx;
						margin-left: auto;
					}
				}
			}
		}
	}
</style>
